﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Windows.Report
{
    public partial class frmBaoCaoCTCNKH : DevExpress.XtraEditors.XtraForm
    {
        Dictionary<string, object> _valueList = new Dictionary<string, object>();
        string _loaict = "";
        string _makh = "";
        string _tenkh = "";

        public string Tenkh
        {
            get { return _tenkh; }
            set { _tenkh = value; }
        }

        public string Makh
        {
            get { return _makh; }
            set { _makh = value; }
        }

        public string Loaict
        {
            get { return _loaict; }
            set { _loaict = value; }
        }

        public Dictionary<string, object> ValueList
        {
            get { return _valueList; }
            set { _valueList = value; }
        }
        DataSet _ds = null;
        public DataSet DataSource { get { return _ds; } }


        public frmBaoCaoCTCNKH()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }
        public DataSet BuildDataSourceNXHD()
        {
            string query = "";

            query = @"SELECT     SUM(CONGNO.SOTIEN) AS SOTIEN, CONGNO.MAKH, CUSTINFO.TENGD, CUSTINFO.DIACHI
FROM         CONGNO INNER JOIN
                      CUSTINFO ON CONGNO.MAKH = CUSTINFO.CUSTID
WHERE     (CONGNO.NGAYCT < '{0:yyyy-MM-dd}') AND (CONGNO.SOCTTT = '{2}') AND (CONGNO.MAKH = '{3}')
GROUP BY CONGNO.MAKH, CUSTINFO.TENGD, CUSTINFO.DIACHI


SELECT     GIAODICH.NGAYCT, GIAODICH.SOCT,isnull( NXHANG.LINE,'') as LINE , GIAODICH.LOAICT,isnull( NXHANG.LOAIHANG,'') as LOAIHANG ,isnull( NXHANG.LOCATION,'') as LOCATION,isnull( NXHANG.QUYCACH,'') as QUYCACH,isnull( NXHANG.DVTINH,'') as DVTINH, 
                    isnull( NXHANG.SOLUONG,0) as  SOLUONG, isnull( NXHANG.GIAVON,0) as  GIAVON,isnull( NXHANG.GIABAN,0) as GIABAN,isnull( NXHANG.SOTIEN,0) as SOTIEN,  GIAODICH.TIENHANG, GIAODICH.PHIVC, 
                      GIAODICH.CHIETKHAU, GIAODICH.TIENMAT, GIAODICH.TONGTIEN, GIAODICH.CONGNO, GIAODICH.THUNGAY,
                          (SELECT     SUM(SOTIEN) AS Expr1
                            FROM          CONGNO
                            WHERE      (NGAYCT <= GIAODICH.NGAYCT) AND (SOCTTT = '{2}') AND (MAKH = '{3}')) AS SOTIENCONNO,
isnull( LOAIHANG.TENHANG,N'Trả tiền hàng') as TENHANG
FROM         LOAIHANG INNER JOIN
                      NXHANG ON LOAIHANG.MAHANG = NXHANG.LOAIHANG RIGHT OUTER JOIN
                      GIAODICH ON NXHANG.SOCT = GIAODICH.SOCT AND NXHANG.LOAICT = GIAODICH.LOAICT AND NXHANG.NGAYCT = GIAODICH.NGAYCT
WHERE     (GIAODICH.NGAYCT BETWEEN '{0:yyyy-MM-dd}' AND '{1:yyyy-MM-dd}') AND (GIAODICH.LOAICT IN ('BH001','NH001','UNC01', 'UNC02')) AND (GIAODICH.MAKH = '{3}')";
            query = string.Format(query, txtDateF.EditValue, txtDateT.EditValue, Loaict, Makh);

            BUS.CommonControl ctr = new BUS.CommonControl();
            _ds = ctr.executeSelectQueries(query);
            return _ds;
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            _ds = BuildDataSourceNXHD();

            if (_ds.Tables.Count == 0)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            bool isdata = false;
            foreach (DataTable dt in _ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    isdata = true;
                    break;
                }
            }
            if (isdata == false)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            _valueList.Add("DateF", txtDateF.EditValue);
            _valueList.Add("DateT", txtDateT.EditValue);
            _valueList.Add("TenKH", Tenkh);
            _valueList.Add("LoaiKH", Loaict);
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }


        private void frmBangKeGTGT_Load(object sender, EventArgs e)
        {
            txtDateF.EditValue = new DateTime(frmMain.__ngayMoSo.Year, frmMain.__ngayMoSo.Month, 1);
            txtDateT.EditValue = frmMain.__ngayMoSo;
            lbTENKH.Text = Tenkh;
        }
    }
}